package com.peanut.controller;

import com.peanut.common.Result;
import com.peanut.domain.po.TabUser;
import com.peanut.em.ResultEnum;
import com.peanut.service.UserManagerService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("userManager")
public class UserManagerController {

    @Autowired
    private UserManagerService userManagerService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    //展示用户信息
    @GetMapping("findUser")
    public Result findUser(){
        TabUser user = new TabUser();
        List<TabUser> userList = userManagerService.findAllUser(user);
        return Result.OK().setData(userList);
    }
    //回显用户信息
    @PostMapping("echoUser")
    public Result echoUser(@RequestBody TabUser user){
        List<TabUser> userList = userManagerService.findUser(user);
        return Result.OK().setData(userList);
    }
    //修改用户信息
    @PutMapping("updateUser")
    @ResponseBody
    public Result updateUser(@RequestBody TabUser user,@RequestParam("phone") String phone){
        int i = userManagerService.updateUser(user);
        if (i > 0){
            return Result.OK();
        }else {
            return Result.NO();
        }
    }


    //接收手机号
    @RequestMapping("message")
    @ResponseBody
    public Result message(@RequestParam("phone")String phone){
        try {
            rocketMQTemplate.convertAndSend("phoneNum_Topic",phone);

            return Result.R(ResultEnum.SUCCESS);
        } catch (MessagingException e) {
            e.printStackTrace();
            return Result.R(ResultEnum.ERROR);
        }
    }
    //删除用户信息
    @DeleteMapping("deleteUser")
    public Result deleteUser(@RequestBody TabUser user){
        userManagerService.deleteUser(user);
        return Result.OK();
    }
}
